package video;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import database.Reader;

/**
 * Class that configures the playlist based on List of paths.
 * 
 * @author Heitor Meira de Melo
 * @version 0.1 December 03, 2009
 * 
 */
public class VideoOrganizer {
	private Reader database;

	/**
	 * Constructor of a VideoOrganizer.
	 * 
	 * @param reader
	 *            An instance of the Reader class.
	 */
	public VideoOrganizer(Reader reader) {
		database = reader;
	}

	/**
	 * Organize the three kinds of sponsor List to play.
	 * 
	 * @return The organized list.
	 */
	private List<String> organizeSponsorslist() {
		List<String> sponsorList = new ArrayList<String>();

		List<String> platinum = database.getPathWithSponsorTypePlatinum();
		List<String> gold = database.getPathWithSponsorTypeGold();
		List<String> silver = database.getPathWithSponsorTypeSilver();

		int indexPlatinum = 0;
		int indexGold = 0;
		int indexSilver = 0;

		Iterator<String> itMaxList = maxList(platinum, gold, silver).iterator();

		while (itMaxList.hasNext()) {
			addRightVideo(platinum, indexPlatinum, sponsorList);
			addRightVideo(gold, indexGold, sponsorList);
			addRightVideo(silver, indexSilver, sponsorList);

			indexPlatinum += 1;
			indexGold += 1;
			indexSilver += 1;

			itMaxList.next();
		}
		return sponsorList;
	}

	/**
	 * Return the organized playlist.
	 * 
	 * @return The playlist.
	 */
	public List<String> getOrganizedPlayList() {
		return organizeList();
	}

	/**
	 * organize the sponsors playlist with news and entertainment list.
	 * 
	 * @return The total organized playlist.
	 */
	private List<String> organizeList() {
		List<String> entertainment = database.getEntertainmentVideos();

		List<String> sponsorList = organizeSponsorslist();

		Iterator<String> itMaxList = maxList(entertainment, sponsorList, null)
				.iterator();

		List<String> organizedList = new ArrayList<String>();

		int entertainmentIndex = 0;
		int sponsorsIndex = 0;

		while (itMaxList.hasNext()) {

			addRightVideo(entertainment, entertainmentIndex, organizedList);
			addRightVideo(sponsorList, sponsorsIndex, organizedList);

			organizedList.add("news");

			entertainmentIndex += 1;
			sponsorsIndex += 1;

			itMaxList.next();
		}
		return organizedList;
	}

	/**
	 * manager the index of a list to add a right video i the playlist.
	 * 
	 * @param sponsor
	 *            List to get the path.
	 * @param index
	 *            current index.
	 * @param organizedList
	 *            List to add the path.
	 */
	private void addRightVideo(List<String> sponsor, int index,
			List<String> organizedList) {
		if (index >= sponsor.size()) {
			index = 0;
		}

		if (!sponsor.isEmpty()) {
			organizedList.add(sponsor.get(index));
		}

	}

	/**
	 * Return the max list.
	 * 
	 * @param list1
	 *            A list.
	 * @param list2
	 *            A list.
	 * @param list3
	 *            A list.
	 * @return The max list list.
	 */
	private List<String> maxList(List<String> list1, List<String> list2,
			List<String> list3) {
		List<String> maxList = list1;
		if (list2.size() >= maxList.size()) {
			maxList = list2;
		}

		try {
			if (list3.size() >= maxList.size()) {
				maxList = list3;
			}
		} catch (Exception ex) {

		}

		return maxList;
	}

}
